package cn.pro.web;

import cn.pro.domain.Product;
import cn.pro.service.ProductService;
import cn.pro.service.impl.ProductServceImpl;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Show {
    public static void main(String[] args) throws IOException {
        //通过键盘录入Scanner
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要选择的功能：1导入2导出");
        int num = sc.nextInt();
        ProductService productService = new ProductServceImpl();
        if (num == 1) {
            //1.导入
            //1.1读取excel表中的数据
            System.out.println("请输入您要读取的文件位置（不包含空格）");
            String path = sc.next();
            List<Product> productlist = read(path);
//            System.out.println(productlist);
            //1.2将数据写入到数据库中

            productService.save(productlist);
            System.out.println("保存数据库成功");

        } else if (num == 2) {
            //2.导出
            //2.1读取数据库中的数据
            List<Product> productlist = productService.findAll();
            System.out.println(productlist);
            //2.2将数据写入到excel表格中
            System.out.println("请输入你要存储的excel路径以及名称");
            String path = sc.next();
            write(productlist,path);
            System.out.println("写入成功");

        } else {
            System.out.println("输入有误，请重新启动");
        }
    }

    public static List<Product> read(String path) throws IOException {
        List<Product> productlist = new ArrayList<Product>();
        //        1.获取工作铺
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(path);
        //2.获取工作表
        XSSFSheet sheet = xssfWorkbook.getSheetAt(0);

        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i <= lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            if (row != null) {
                List<String> list = new ArrayList<String>();

                for (Cell cell : row) {
                    if (cell != null) {
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        String value = cell.getStringCellValue();
                        if(value!=null&&!"".equals(value)){
                            list.add(value);
                        }

                    }
                }
                if(list.size()>0){
                    Product product = new Product(Integer.parseInt(list.get(0)), list.get(1), Double.parseDouble(list.get(2)), Integer.parseInt(list.get(3)));
                    productlist.add(product);
                }

            }
        }
        return productlist;
    }
    public  static void write(List<Product> productList,String path) throws IOException {
//        1.创建工作铺
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
//        2.创建工作表
        XSSFSheet sheet = xssfWorkbook.createSheet("商品");

//        创建单元格样式
        XSSFCellStyle xssfCellStyle = xssfWorkbook.createCellStyle();
        xssfCellStyle.setFillForegroundColor(IndexedColors.PINK.getIndex());
        xssfCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        //字体样式
        XSSFFont font = xssfWorkbook.createFont();
        font.setFontName("黑体");
        font.setColor(IndexedColors.BLUE.getIndex());
        xssfCellStyle.setFont(font);
//        3.创建行
        XSSFRow row = sheet.createRow(0);
//        row.createCell(0).setCellValue("商品编号");
//        row.createCell(1).setCellValue("商品名称");
//        row.createCell(2).setCellValue("商品价格（单位：元/斤）");
//        row.createCell(3).setCellValue("商品库存（单位:吨）");
        XSSFCell cell1 = row.createCell(0);
        cell1.setCellStyle(xssfCellStyle);
        cell1.setCellValue("商品编号");

        XSSFCell cell2 = row.createCell(1);
        cell2.setCellStyle(xssfCellStyle);
        cell2.setCellValue("商品名称");

        XSSFCell cell3 = row.createCell(2);
        cell3.setCellStyle(xssfCellStyle);
        cell3.setCellValue("商品价格（单位：元/斤）");

        XSSFCell cell4 = row.createCell(3);
        cell4.setCellStyle(xssfCellStyle);
        cell4.setCellValue("商品库存（单位:吨）");

        for(int i = 0;i<productList.size();i++){
            XSSFRow row1 = sheet.createRow(i+1);
            row1.createCell(0).setCellValue(productList.get(i).getPid());
            row1.createCell(1).setCellValue(productList.get(i).getPname());
            row1.createCell(2).setCellValue(productList.get(i).getPrice());
            row1.createCell(3).setCellValue(productList.get(i).getPstock());
        }
        FileOutputStream fileOutputStream = new FileOutputStream(path);
        xssfWorkbook.write(fileOutputStream);
        fileOutputStream.flush();

        fileOutputStream.close();
        xssfWorkbook.close();
    }
}
